<?php

/**
 * TestDbSqlite3Adapter class
 * Testing unit of 'phpraise/db/Sqlite3Adapter.php'
 *
 * @author Sam-Mauris Yong / hellclanner at live dot com
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package Raise.UnitTest.Test
 */
class TestDbSqlite3Adapter extends RaiseTest {
    
    public static function testRun(){
        $settings = new RaiseSettings();
        $settings->set('Database', 'adapterMap', 'mysql=MySqlAdapter&sqlite=SqliteAdapter&sqlite3=Sqlite3Adapter');

        $conn = new RaiseDbConnection($settings, 'sqlite3:host=mocks/testdb.sqlite');

        self::assertClassExists('Sqlite3Adapter');

        $cmd = new RaiseDbCommand($conn, 'SELECT COUNT(UserId) FROM User');
        $count = $cmd->executeScalar();
        self::assertEqual($count, 3);

        $cmd = new RaiseDbCommand($conn, 'SELECT Username FROM User WHERE UserId = @id');
        $cmd->params()->add('@id', 2);
        $name = $cmd->executeScalar();
        self::assertEqual($name, 'thephpdeveloper');

        $cmd = new RaiseDbCommand($conn, 'SELECT * FROM User WHERE UserId = @id');
        $cmd->params()->add('@id', 1);
        $sobj = $cmd->executeFetchSingleObject();
        self::assertObjectClass($sobj, 'stdClass');
        self::assertStrictEqual($sobj->UserId, 1);
        self::assertStrictEqual($sobj->Username, 'mauris');

        $cmd = new RaiseDbCommand($conn, 'SELECT * FROM User WHERE UserId = @id');
        $cmd->params()->add('@id', 3);
        $sarr = $cmd->executeFetchSingleArray();
        self::assertType($sarr, 'array');
        self::assertStrictEqual($sarr[0], 3);
        self::assertStrictEqual($sarr[1], 'testUser');

        $cmd = new RaiseDbCommand($conn, 'SELECT Username FROM User');
        $values = $cmd->executeFetchArrayTable();
        $testAgainstArr = array(
            array('mauris'),
            array('thephpdeveloper'),
            array('testUser')
        );
        self::assertEqual($values, $testAgainstArr);

        $cmd = new RaiseDbCommand($conn, 'SELECT Username FROM User');
        $values = $cmd->executeFetchObject();
        self::assertStrictEqual(count($values), 3);
        foreach($values as $v){
            self::assertObjectClass($v, 'stdClass');
        }
    }

}
